﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HdipCricketClub.Models;
using HdipCricketClub.ViewModels;

namespace HdipCricketClub.Controllers
{
    public class ResultsController : Controller
    {
        CricketClubEntities db = new CricketClubEntities();

        FixtureRepository fixtureRepository = new FixtureRepository();
        ResultRepository resultRepository = new ResultRepository();

        //
        // GET: /Results/

        public ActionResult Index()
        {
            return View();
        }

        //
        //  GET:  /Results/ViewSummaries

        public ViewResult ViewSummaries()
        {
            var results = resultRepository.FindAllResults();

            return View(results);
        }
        
        //
        //  GET: /Results/ViewScoreCards/

        //public ActionResult ViewScorecards()
        //{
        //    var fixtures = fixtureRepository.FindFixtureScorecards();

        //    return View(fixtures.ToList());
        //}

        //
        //  GET: /Results/GetScorecard/

        public ActionResult GetScorecard(int id)
        {
            CompletedScorecardViewModel completedScorecardViewModel = new CompletedScorecardViewModel();

            completedScorecardViewModel.ClubBatsmen = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == id).OrderBy(cfp => cfp.batsman_number).ToList();

            double subTotalClubBatsmen = 0;

            foreach (var player in completedScorecardViewModel.ClubBatsmen)
            {
                if (player.batting_runs != null)
                {
                    subTotalClubBatsmen += (double)player.batting_runs;
                }
            }

            ViewBag.SubTotalClubBatsmen = subTotalClubBatsmen;

            completedScorecardViewModel.ClubBowlers = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == id && cfp.bowler_number > 0).OrderBy(cfp => cfp.bowler_number).ToList();

            completedScorecardViewModel.ClubOtherDetail = db.ClubOtherFixtureDetails.Where(cod => cod.fixture_id == id).Single();

            double totalClubExtras = (double)completedScorecardViewModel.ClubOtherDetail.byes
                                   + (double)completedScorecardViewModel.ClubOtherDetail.leg_byes
                                   + (double)completedScorecardViewModel.ClubOtherDetail.wides
                                   + (double)completedScorecardViewModel.ClubOtherDetail.no_balls;

            ViewBag.TotalClubExtras = totalClubExtras;
            ViewBag.TotalClubRuns = subTotalClubBatsmen + totalClubExtras;

            completedScorecardViewModel.OpponentBatsmen = db.OpponentFixturePlayers.Where(ofp => ofp.op_fixture_id == id).OrderBy(ofp => ofp.op_batsman_number).ToList();

            double subTotalOpponentBatsmensRuns = 0;

            foreach (var player in completedScorecardViewModel.OpponentBatsmen)
            {
                if (player.op_batting_runs != null)
                {
                    subTotalOpponentBatsmensRuns += (double)player.op_batting_runs;
                }
            }

            ViewBag.SubTotalOpponentBatsmensRuns = subTotalOpponentBatsmensRuns;

            completedScorecardViewModel.OpponentBowlers = db.OpponentFixturePlayers.Where(ofp => ofp.op_fixture_id == id && ofp.op_bowler_number > 0).OrderBy(ofp => ofp.op_bowler_number).ToList();

            completedScorecardViewModel.OpponentOtherDetail = db.OpponentOtherFixtureDetails.Where(ood => ood.fixture_id == id).Single();

            double totalOpponentExtras = (double)completedScorecardViewModel.OpponentOtherDetail.byes
                                       + (double)completedScorecardViewModel.OpponentOtherDetail.leg_byes
                                       + (double)completedScorecardViewModel.OpponentOtherDetail.wides
                                       + (double)completedScorecardViewModel.OpponentOtherDetail.no_balls;

            ViewBag.TotalOpponentExtras = totalOpponentExtras;
            ViewBag.TotalOpponentRuns = subTotalOpponentBatsmensRuns + totalOpponentExtras;
            
            completedScorecardViewModel.ScorecardFixture = fixtureRepository.GetFixture(id);

            return View(completedScorecardViewModel);
        }

        //
        //  GET:    Results/ViewTables/

        public ActionResult ViewTables()
        {
            return View();
        }
        



    }
}
